<?php
defined('_JEXEC') or die('Restricted access'); 
 
jimport( 'joomla.html.html.tabs' );
JHtml::_('jquery.framework');
JHtml::_('behavior.keepalive');
JHTML::_('behavior.tooltip'); 
JHtml::_('behavior.framework');
?>

<script type="text/javascript">
	//<![CDATA[
	Joomla.submitbutton = function(pressbutton){
		if (pressbutton == 'redirect_to_profile' || pressbutton == 'products_page') {
			submitform( pressbutton );
			return;
		}
		if (pressbutton == 'save_new_redemption') {
			if (document.adminForm.cat_id.value == 0) {
				alert( 'Please select a category !' );
				document.adminForm.cat_id.focus();
				return false;
			}
			if (document.adminForm.quantity.value == '') {
				alert( 'Must have quantity !' );
				document.adminForm.quantity.focus();
				return false;
			}
			if (isNaN(document.adminForm.quantity.value)) {
				alert( 'Quantity have to a number !' );
				document.adminForm.quantity.focus();
				return false;
		    }
			submitform( pressbutton );
		}
	}
	
	var sectioncategory = new Array;
	<?php
		foreach ($this->lists['cat_to_product'] as $k=>$item) {
			echo "sectioncategory[".$k."] = new Array( '$item->cat_id','".addslashes( $item->id )."','".addslashes( $item->name )."' );\n\t\t";
		}
	?>

	function changeProducts( listname, source, key, orig_key, orig_val ) {
		var list = eval( 'document.adminForm.' + listname );

		// empty the list
		for (i in list.options.length) {
			list.options[i] = null;
		}

		var i = 0;
		var j = 0;
		for (x in source) {
			if (source[x][0] == key) {
				opt = new Option();
				opt.value = source[x][1];

				opt.text = source[x][2];

				if ((orig_key == key && orig_val == opt.value) || i == 0) {
					opt.selected = true;
					j = opt.value;
				}
				list.options[i++] = opt;
			}
		}
		if(i == 0){
			opt = new Option();
			opt.value = 0;
			opt.text = '- Select Products -';
			opt.selected = true;
			list.options[i++] = opt;
		}
		list.length = i;
		jQuery.noConflict();
	    jQuery(document).ready(function(){
	        jQuery("#product_id").change(function(){
	            jQuery("#product_info").text('Loading...').load('<?php print JRoute::_('index.php?option=com_wrd&controller=profile&layout=pinfo&tmpl=component',false); ?>',{'pid':jQuery("#product_id").val()});
	        })
	        jQuery("#product_info").text('Loading...').load('<?php print JRoute::_('index.php?option=com_wrd&controller=profile&layout=pinfo&tmpl=component',false); ?>',{'pid':jQuery("#product_id").val()});
	    });
	}
	
	//<![CDATA[
		jQuery.noConflict();
	    jQuery(document).ready(function(){
	        jQuery("#product_id").change(function(){
	            jQuery("#product_info").text('Loading...').load('<?php print JRoute::_('index.php?option=com_wrd&controller=profile&layout=pinfo&tmpl=component',false); ?>',{'pid':jQuery("#product_id").val()});
	        })
	        jQuery("#product_info").text('Loading...').load('<?php print JRoute::_('index.php?option=com_wrd&controller=profile&layout=pinfo&tmpl=component',false); ?>',{'pid':jQuery("#product_id").val()});

	        jQuery("#quantity").change(function(){
	        	var cat_id = jQuery("#cat_id").val() ;
	        	var product_id = jQuery("#product_id").val() ;
	        	var p_o_u = jQuery("#p_o_u").val() ;
		        var quantity = jQuery("#quantity").val() ;
		        var p_quantity = jQuery("#p_quantity").val() ;
		        var pointx = jQuery("#pointx").val() ;
		        var cash = jQuery("#cash").val() ;
				if(parseInt(cat_id) == '' || parseInt(cat_id) == 0){
					alert('Please choose a category') ;
				}else if(parseInt(product_id) == '' || parseInt(product_id) == 0){
					alert('Please choose a product') ;
				}else{
					if(parseInt(quantity) <= 0 ){
						alert('Quantity is not correct') ;
					}else{
						if(parseInt(quantity) > parseInt(p_quantity )){
							alert('Quantity must be less than (or equal to) '+parseInt(p_quantity));
						}else{
							var points_cost = quantity * pointx ;
							if(parseInt(points_cost) > parseInt(p_o_u)){
								alert('Not enought points for this exchange') ;
							}
						}
					}
				}
	        });
	    });
	// ]]>
</script>
<form  id="adminForm" action="<?php echo JRoute::_("index.php?option=com_wrd")?>" method="post" name="adminForm" >
	<div class="col width-60" style="float:left; width:60%">
	    <fieldset class="adminform">
	    	<legend><?php echo JText::_('Details')?></legend>
			<table width="100%">
				<tr>
					<td class="key">
						<label for="title"><?php echo JText::_('Guest Name')?>:</label>
					</td>
					<td colspan="2">
						<?php
							if(!empty($this->user_info->f_name) || !empty($this->user_info->l_name)){
								$guest_name = $this->user_info->f_name." ".$this->user_info->l_name ;
							}else{
								$guest_name = $this->data->guest_name ;
							}
						?>
						<input type="text" title="type" maxlength="50" size="50" value="<?php echo $guest_name ; ?>" id="guest_name" name="guest_name" class="guest_name"/> 
					</td>
				</tr>
				<tr>
					<td class="key">
						<label for="title"><?php echo JText::_('Category')?>:</label>
					</td>
					<td colspan="2">
						<?php echo $this->lists['category']; ?>
					</td>
				</tr>
				<tr>
					<td class="key">
						<label for="title"><?php echo JText::_('Product')?>:</label>
					</td>
					<td colspan="2">
						<?php echo $this->lists['products']; ?>
					</td>
				</tr>
				<tr>
					<td class="key">
						<label for="title"><?php echo JText::_('Quantity')?>:</label>
					</td>
					<td colspan="2">
						<input type="text" title="type" maxlength="50" size="50" value="" id="quantity" name="quantity" class="quantity"/> 
					</td>
				</tr>
				<tr>
					<td class="key">
						<label for="title"><?php echo JText::_('Type Redemption')?>:</label>
					</td>
					<td colspan="2">
						<table>
							<tr>
								<td>
									<input type="radio" checked="checked" value="1" id="point" name="type_redeem" class="point" />
								</td>
								<td>Point</td>
								<td>
									<input type="radio" value="2" id="point_cash" name="type_redeem" class="point_cash" />
								</td>	
								<td>Point and Cash</td>
							</tr>
						</table>
					</td>
				</tr>
            </table>
		</fieldset>
	</div>
	<?php if(!empty($this->user_info)):?>
	<div id="stats" style="float:right;width:40%">
		<?php
			jimport('joomla.html.pane');
			$pane =& JPane::getInstance('Tabs');
				echo $pane->startPane('myPane');
				echo $pane->startPanel(JText::_('User Infomation'), 'infotab');
				?>
					<table>
						<tr>
							<td><?php echo JText::_('Name') ;?></td>
							<td>:</td>
							<td>
								<?php
									if(!empty($this->user_info->f_name) || !empty($this->user_info->l_name)){
										$guest_name = $this->user_info->f_name." ".$this->user_info->l_name ;
									}else{
										$guest_name = '' ;
									}
									echo empty($guest_name)? '' : $this->user_info->prefix.' '.$guest_name ; 
								?> 
							</td>
						</tr>
						<tr>
							<td><?php echo JText::_('Date of Birth') ;?></td>
							<td>:</td>
							<td>
								<?php
									echo $this->user_info->dob ; 
								?> 
							</td>
						</tr>
						<tr>
							<td><?php echo JText::_('Job Title') ;?></td>
							<td>:</td>
							<td>
								<?php  echo empty($this->job) ? '' : $this->job ; ?> 
							</td>
						</tr>
						<tr>
							<td><?php echo JText::_('Member Joining Date') ;?></td>
							<td>:</td>
							<td>
								<?php
									echo $this->user_info->created_date ; 
								?> 
							</td>
						</tr>
						<tr>
							<td><?php echo JText::_('Outlet') ;?></td>
							<td>:</td>
							<td>
								<?php
									echo empty($this->outlet->name) ? '' : $this->outlet->name ; 
								?> 
							</td>
						</tr>
						<tr>
							<td><?php echo JText::_('Current points balance') ;?></td>
							<td>:</td>
							<td>
								<?php echo $this->p_o_u ; ?> 
								<input type="hidden" name='p_o_u' id='p_o_u' value='<?php echo $this->p_o_u ;?>' />
							</td>
						</tr>
					</table>
				<?php 
				echo $pane->endPanel();
			
			echo $pane->endPane();
		?>
	</div>
	<?php endif ; ?>
	<div id="stats" style="float:right;margin: 10px 0 ; width:40%">
		<div id='product_info'>&nbsp;</div>
	</div>
	<input type="hidden" name="controller" value="profile" />
	<input type="hidden" name="option" value="com_wrd" />
	<input type="hidden" name="uid" value="<?php echo (int)$this->user_info->user_id; ?>" />
	<input type="hidden" name="task" value="" />
	<?php echo JHTML::_( 'form.token' ); ?>
</form>
